<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyWord" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60" label="序号">
      </el-table-column>
      <el-table-column prop="sn" label="唯一标识" width="240" >
      </el-table-column>
      <el-table-column prop="title" label="标题" width="240" >
      </el-table-column>
      <el-table-column prop="intro" label="描述" min-width="200" >
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" :current-page="query.currentPage" @current-change="handleCurrentChange" :page-size="query.pageSize" :total="pageInfo.total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增/修改界面-->
    <el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm" >
        <el-form-item label="唯一标识" >
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="标题" >
          <el-input v-model="saveForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input type="textarea" v-model="saveForm.intro"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="save" :loading="addLoading" >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

export default {
  data() {
    return {
      listLoading: false,
      sels: [],//列表选中列
      query:{
        currentPage:1,
        pageSize:10,
        keyWord:''
      },
      pageInfo:{
        total:0,
        rows:[]
      },
      saveFormVisible: false,//编辑界面是否显示
      addLoading: false,
      saveFormRules: {
        name: [
          { required: true, message: '请输入名字', trigger: 'blur' }
        ]
      },
      //新增界面数据
      saveForm: {
        id:null,
        sn:'',
        title:'',
        intro:""
      },
      employees:[],
      deptTree:[],
    }
  },
  methods: {
    handleCurrentChange(val) {
      console.log(val)
      this.query.currentPage = val;
      this.getDictionarys();
    },
    search(){
      this.query.currentPage=1;
      this.getDictionarys();
    },
    //获取部门列表
    getDictionarys() {
      this.listLoading = true;
      console.log(this.query)
      this.$http.post("/dictionary",this.query)
          .then(result => {
            console.log(result)
            result = result.data;
            if (result.success){
              this.pageInfo=result.data;
              this.listLoading = false;
            }else{
              this.$message({message:result.message,type:"error"});
            }
          })
          .catch(result =>{
            this.$message({message:"网络异常,请检查网络!",type:"error"});
          })
    },
    //单条删除
    handleDel: function (index, row) {
      console.log(row)
      this.$confirm('确认删除该数据吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.delete("/dictionary/"+row.id)
            .then(result => {
              console.log(result);
              result = result.data;
              if (result.success){
                this.$message({message:"删除成功!",type:"success"});
                this.query.currentPage=1;
                this.getDictionarys();
              }else{
                this.$message({message:result.message,type:"error"});
              }
            })
            .catch(result =>{
              this.$message({message:"网络异常,请检查网络!",type:"error"});
            })
      }).catch(() => {
        this.$message({message:"已取消删除!",type:"info"});
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.saveForm = {
        id:null,
        sn:'',
        title:'',
        intro:""
      }
    },
    //新增/修改
    save:function() {
      this.$refs.saveForm.validate((valid) => {
        console.log("valid"+valid)
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {})
              .then(() => {
                this.addLoading = true;
                let param = this.saveForm;
                this.$http.put("/dictionary",param)
                    .then(result => {
                      console.log(result);
                      result = result.data;
                      if (result.success){
                        this.addLoading = false;
                        this.saveFormVisible=false;
                        this.$message({message:"保存成功!",type:"success"});
                        this.query.currentPage=1;
                        this.getDictionarys();
                      }else{
                        this.$message({message:result.message,type:"error"});
                      }
                    })
                    .catch(result =>{
                      this.$message({message:"网络异常,请检查网络!",type:"error"});
                    })
              }).catch(result =>{
            this.$message({message:"已取消保存!",type:"info"});
          })
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      console.log(ids)
      this.$confirm('确认删除多条数据吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.patch("/dictionary/",ids)
            .then(result => {
              console.log(result);
              result = result.data;
              if (result.success){
                this.$message({message:"批量删除成功!",type:"success"});
                this.query.currentPage=1;
                this.getDictionarys();
              }else{
                this.$message({message:result.message,type:"error"});
              }
            })
            .catch(result =>{
              this.$message({message:"网络异常,请检查网络!",type:"error"});
            })
      }).catch(() => {
        this.$message({message:"已取消批量删除!",type:"info"});
      });
    }
  },
  mounted() {
    this.getDictionarys();
  }
}

</script>

<style scoped>

</style>